/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mesaayuda.mrysi.DAO;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import mesaayuda.mrysi.model.Departamento;
import mesaayuda.mrysi.model.Ticket;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import mesaayuda.mrysi.DAO.exceptions.NonexistentEntityException;
import mesaayuda.mrysi.DAO.exceptions.PreexistingEntityException;
import mesaayuda.mrysi.model.Topicoayuda;

/**
 *
 * @author Gio
 */
public class TopicoayudaJpaController implements Serializable {

    public TopicoayudaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Topicoayuda topicoayuda) throws PreexistingEntityException, Exception {
        if (topicoayuda.getTicketCollection() == null) {
            topicoayuda.setTicketCollection(new ArrayList<Ticket>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Departamento iddepartamento = topicoayuda.getIddepartamento();
            if (iddepartamento != null) {
                iddepartamento = em.getReference(iddepartamento.getClass(), iddepartamento.getIddepartamento());
                topicoayuda.setIddepartamento(iddepartamento);
            }
            Collection<Ticket> attachedTicketCollection = new ArrayList<Ticket>();
            for (Ticket ticketCollectionTicketToAttach : topicoayuda.getTicketCollection()) {
                ticketCollectionTicketToAttach = em.getReference(ticketCollectionTicketToAttach.getClass(), ticketCollectionTicketToAttach.getIdticket());
                attachedTicketCollection.add(ticketCollectionTicketToAttach);
            }
            topicoayuda.setTicketCollection(attachedTicketCollection);
            em.persist(topicoayuda);
            if (iddepartamento != null) {
                iddepartamento.getTopicoayudaCollection().add(topicoayuda);
                iddepartamento = em.merge(iddepartamento);
            }
            for (Ticket ticketCollectionTicket : topicoayuda.getTicketCollection()) {
                Topicoayuda oldIdtopicoayudaOfTicketCollectionTicket = ticketCollectionTicket.getIdtopicoayuda();
                ticketCollectionTicket.setIdtopicoayuda(topicoayuda);
                ticketCollectionTicket = em.merge(ticketCollectionTicket);
                if (oldIdtopicoayudaOfTicketCollectionTicket != null) {
                    oldIdtopicoayudaOfTicketCollectionTicket.getTicketCollection().remove(ticketCollectionTicket);
                    oldIdtopicoayudaOfTicketCollectionTicket = em.merge(oldIdtopicoayudaOfTicketCollectionTicket);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findTopicoayuda(topicoayuda.getIdtopicoayuda()) != null) {
                throw new PreexistingEntityException("Topicoayuda " + topicoayuda + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Topicoayuda topicoayuda) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Topicoayuda persistentTopicoayuda = em.find(Topicoayuda.class, topicoayuda.getIdtopicoayuda());
            Departamento iddepartamentoOld = persistentTopicoayuda.getIddepartamento();
            Departamento iddepartamentoNew = topicoayuda.getIddepartamento();
            Collection<Ticket> ticketCollectionOld = persistentTopicoayuda.getTicketCollection();
            Collection<Ticket> ticketCollectionNew = topicoayuda.getTicketCollection();
            if (iddepartamentoNew != null) {
                iddepartamentoNew = em.getReference(iddepartamentoNew.getClass(), iddepartamentoNew.getIddepartamento());
                topicoayuda.setIddepartamento(iddepartamentoNew);
            }
            Collection<Ticket> attachedTicketCollectionNew = new ArrayList<Ticket>();
            for (Ticket ticketCollectionNewTicketToAttach : ticketCollectionNew) {
                ticketCollectionNewTicketToAttach = em.getReference(ticketCollectionNewTicketToAttach.getClass(), ticketCollectionNewTicketToAttach.getIdticket());
                attachedTicketCollectionNew.add(ticketCollectionNewTicketToAttach);
            }
            ticketCollectionNew = attachedTicketCollectionNew;
            topicoayuda.setTicketCollection(ticketCollectionNew);
            topicoayuda = em.merge(topicoayuda);
            if (iddepartamentoOld != null && !iddepartamentoOld.equals(iddepartamentoNew)) {
                iddepartamentoOld.getTopicoayudaCollection().remove(topicoayuda);
                iddepartamentoOld = em.merge(iddepartamentoOld);
            }
            if (iddepartamentoNew != null && !iddepartamentoNew.equals(iddepartamentoOld)) {
                iddepartamentoNew.getTopicoayudaCollection().add(topicoayuda);
                iddepartamentoNew = em.merge(iddepartamentoNew);
            }
            for (Ticket ticketCollectionOldTicket : ticketCollectionOld) {
                if (!ticketCollectionNew.contains(ticketCollectionOldTicket)) {
                    ticketCollectionOldTicket.setIdtopicoayuda(null);
                    ticketCollectionOldTicket = em.merge(ticketCollectionOldTicket);
                }
            }
            for (Ticket ticketCollectionNewTicket : ticketCollectionNew) {
                if (!ticketCollectionOld.contains(ticketCollectionNewTicket)) {
                    Topicoayuda oldIdtopicoayudaOfTicketCollectionNewTicket = ticketCollectionNewTicket.getIdtopicoayuda();
                    ticketCollectionNewTicket.setIdtopicoayuda(topicoayuda);
                    ticketCollectionNewTicket = em.merge(ticketCollectionNewTicket);
                    if (oldIdtopicoayudaOfTicketCollectionNewTicket != null && !oldIdtopicoayudaOfTicketCollectionNewTicket.equals(topicoayuda)) {
                        oldIdtopicoayudaOfTicketCollectionNewTicket.getTicketCollection().remove(ticketCollectionNewTicket);
                        oldIdtopicoayudaOfTicketCollectionNewTicket = em.merge(oldIdtopicoayudaOfTicketCollectionNewTicket);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Short id = topicoayuda.getIdtopicoayuda();
                if (findTopicoayuda(id) == null) {
                    throw new NonexistentEntityException("The topicoayuda with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Short id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Topicoayuda topicoayuda;
            try {
                topicoayuda = em.getReference(Topicoayuda.class, id);
                topicoayuda.getIdtopicoayuda();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The topicoayuda with id " + id + " no longer exists.", enfe);
            }
            Departamento iddepartamento = topicoayuda.getIddepartamento();
            if (iddepartamento != null) {
                iddepartamento.getTopicoayudaCollection().remove(topicoayuda);
                iddepartamento = em.merge(iddepartamento);
            }
            Collection<Ticket> ticketCollection = topicoayuda.getTicketCollection();
            for (Ticket ticketCollectionTicket : ticketCollection) {
                ticketCollectionTicket.setIdtopicoayuda(null);
                ticketCollectionTicket = em.merge(ticketCollectionTicket);
            }
            em.remove(topicoayuda);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Topicoayuda> findTopicoayudaEntities() {
        return findTopicoayudaEntities(true, -1, -1);
    }

    public List<Topicoayuda> findTopicoayudaEntities(int maxResults, int firstResult) {
        return findTopicoayudaEntities(false, maxResults, firstResult);
    }

    private List<Topicoayuda> findTopicoayudaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Topicoayuda.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Topicoayuda findTopicoayuda(Short id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Topicoayuda.class, id);
        } finally {
            em.close();
        }
    }

    public int getTopicoayudaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Topicoayuda> rt = cq.from(Topicoayuda.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
